package balking;

public class Data {
    private String content;

    private volatile boolean changed;

    public Data(String content, boolean changed) {
        this.content = content;
        this.changed = changed;
    }

    public synchronized void change(String name, String newContent) {
        System.out.println(name + ":修改了内容为:" + newContent);
        content = newContent;
        changed = true;
    }

    public synchronized void save(String userName) throws InterruptedException {
        if (!changed) {
            System.out.println(userName + ":发现别人在操作,于是默默退出");
            return;
        }
        System.out.println(userName + ":正在执行保存操作");
        Thread.sleep(1000);
        System.out.println(userName + ":已经操作完成");
        changed = false;
    }
}
